const express=require("express");
const app=express();
const dao=require("./db/feng.js");
//id专门转换
const ObjectId = require('mongodb').ObjectID;

const bodyParser=require('body-parser');
let urlencodedParser = bodyParser.urlencoded({ extended: false })

app.use("/",express.static("./html"));
//添加商品
app.post("/add",urlencodedParser,(req,res)=>{
        let obj={
              title:req.body.title,
              price:parseInt(req.body.price),
              numProduct:req.body.numProduct
        }
        dao.insert("product","maquillage",obj,(a)=>{
            res.send(a)
        })
})
//获取页面
app.post("/getProduct",urlencodedParser,(req,res)=>{
    let obj={}
    dao.find("product","maquillage",obj,(a)=>{
        
        res.send(a)
    })
})
//升序
app.post("/up",urlencodedParser,(req,res)=>{
    let obj={};
    let sort={price:parseInt(req.body.price)} 
dao.find("product","maquillage",obj,(as)=>{
    res.send(as);
},sort)


})



//删除
app.post("/del",urlencodedParser,(req,res)=>{
    //console.log(req.body._id);
    let obj={
        _id:new ObjectId(req.body._id)
    };
    dao.delsingle("product","maquillage",obj,(a)=>{
        console.log(a);
        res.send(a);
    })
})
//编辑
app.post("/edit",urlencodedParser,(req,res)=>{
    let oldobj={
        _id:new ObjectId(req.body._id)
    };
    let newobj={
              title:req.body.title,
               price:parseInt(req.body.price),
               numProduct:req.body.numProduct
    };
    console.log(newobj);
    dao.update("product","maquillage",oldobj,newobj,(a)=>{
        res.send(a);
    })
})

//搜索
app.post("/search",urlencodedParser,(req,res)=>{
    var obj={
        title:{$regex:req.body.content},
    }
    console.log(obj);
    dao.find("product","maquillage",obj,(a)=>{
        res.send(a)
    })
})
//分页
app.post("/page",urlencodedParser,(req,res)=>{
    var obj={}
    
        dao.search("product","maquillage",obj,0,0,(a)=>{
            res.send(a)
        })
        
   // })
})


app.listen("8866");